<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title></title>
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <script type="text/javascript" src="../../js/commonCss.js"></script>
    <link rel="stylesheet" type="text/css" href="../../styles/common.css" />
    <style>
        .btn-top{
            position: absolute;
            right: 8%;
            z-index: 9;
        }
    </style>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-body">
        <div class="btn-top">
            <button type="button" class="btn btn-success top-back" id="backbtn">返回</button>
        </div>

        <div class="div-echart ">
            <div id="main" style="width: 100%;height:500px;" class="center-block">
            </div>
        </div>

        <p>采购成本</p>
        <table class="table table-bordered" id="procurement">
            <tr><th>预计采购时间（年）</th><th>预计采购项目</th><th>预计采购金额</th><th>占比</th></tr>
        </table>

        <p>人工成本</p>
        <table class="table table-bordered" id="cost">
            <tr><th>预计发生时间（年）</th><th>使用部门</th><th>人工成本</th><th>占比</th></tr>
        </table>

        <p>直接费用</p>
        <table class="table table-bordered" id="direct">
            <tr><th>预计发生时间（年）</th><th>预计发生费用</th><th>占比</th></tr>
        </table>

        <p>间接费用</p>
        <table class="table table-bordered" id="indirect">
            <tr><th>分摊时间（年）</th><th>分摊金额</th><th>分摊比例</th><th>占比</th></tr>
        </table>
    </div>
</div>

<script type="text/javascript" src="../../js/commonJs.js"></script>
<script type="text/javascript" src="../../js/jquery.formHelp.js"></script>
<script type="text/javascript" src="../../js/echarts.min.js"></script>
<script type="text/javascript" src="../../assets/js/bui.js"></script>
<script type="text/javascript" src="../../assets/js/config-min.js"></script>
<script type="text/javascript">
    BUI.use('common/page');
</script>
<script>
    $("#backbtn").click(function () {
        top.topManager.openPage({
            id : getQueryString('parentPageId'),
            isClose : true
        });
    });

    $(function () {
        var projectIds = [];
        var projectId = getQueryString('projectId');
        var projectName = getQueryString('projectName');
        var needCost = getQueryString('needCost');
        var type = getQueryString('type');
        var dataId = getQueryString('dataId');

        projectIds.push(projectId);

        var seriesData1 = [];
        var seriesData2 = [];
        $.ajax({
            type: "POST",
            url: ctx + "/estimate/queryNeedCost",
            data:{
                projectId: projectIds
            },
            success: function(data){
                if(data.code == 200){
                    var value = data.data;
                    //获取时间范围

                    //采购成本
                    html = '';
                    var totalProcurement = 0;
                    for(var i in value.procurementList){
                        totalProcurement+=value.procurementList[i].amount;
                        html+='<tr><td>'+value.procurementList[i].year+'</td><td>'+value.procurementList[i].temp+'</td><td>'
                            +formatMoney(value.procurementList[i].amount)+'</td><td>'+(value.procurementList[i].amount/needCost*100).toFixed(2)+'%</td></tr>';
                    }
                    if(html==''){
                        html='<tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>';
                    }
                    $('#procurement').append(html);

                    //人工成本
                    html = '';
                    var totalCost = 0;
                    for(var i in value.costList){
                        totalCost+=value.costList[i].amount;
                        html+='<tr><td>'+value.costList[i].year+'</td><td>'+value.costList[i].temp+'</td><td>'
                            +formatMoney(value.costList[i].amount)+'</td><td>'+(value.costList[i].amount/needCost*100).toFixed(2)+'%</td></tr>';
                    }
                    if(html==''){
                        html='<tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>';
                    }
                    $('#cost').append(html);

                    //直接费用
                    var html = '';
                    var totalDirect = 0;
                    for(var i in value.directList){
                        totalDirect+=value.directList[i].amount;
                        html+='<tr><td>'+value.directList[i].year+'</td><td>'+formatMoney(value.directList[i].amount)+'</td><td>'
                            +(value.directList[i].amount/needCost*100).toFixed(2)+'%</td></tr>';
                    }
                    if(html==''){
                        html='<tr><td>-</td><td>-</td><td>-</td></tr>';
                    }
                    $('#direct').append(html);

                    //间接费用
                    html = '';
                    var totalIndirect = 0;
                    for(var i in value.indirectList){
                        if(value.indirectList[i].amount ==0 ||value.indirectList[i].amount == null){
                            continue;
                        }
                        totalIndirect+=value.indirectList[i].amount;
                        html+='<tr><td>'+value.indirectList[i].year+'</td><td>'+formatMoney(value.indirectList[i].amount)+'</td><td>' +Number(value.indirectList[i].temp).toFixed(2)
                            +'</td><td>'+(value.indirectList[i].amount==0?'0.00':(value.indirectList[i].amount/needCost*100).toFixed(2))+'%</td></tr>';
                    }
                    if(html==''){
                        html='<tr><td>-</td><td>-</td><td>-</td><td>-</td></tr>';
                    }
                    $('#indirect').append(html);

                    seriesData1.push({name:"采购成本",value:totalProcurement.toFixed(2)});
                    seriesData1.push({name:"人工成本",value:totalCost.toFixed(2)});
                    seriesData1.push({name:"直接费用",value:totalDirect.toFixed(2)});
                    seriesData1.push({name:"间接费用",value:totalIndirect.toFixed(2)});

                    var yearTotalValue =  value.yearTotal;
                    console.log(yearTotalValue)
                    if(JSON.stringify(yearTotalValue) == "{}"){
                        seriesData2.push({name:'2017',value:0.00});
                    }else {
                        for(var a in yearTotalValue){
                            seriesData2.push({name:a,value:yearTotalValue[a].toFixed(2)});
                        }
                    }
                    setEchart('main');
                }else{
                    alert(data.msg);
                }
            }
        });
        var setEchart = function (eleId) {
            var subtext = '';
            var subLink = '';
            if(type=='project'){
                subtext = '【总金额：'+needCost + '】';
                subLink = 'javascript:showModal()';
                //subLink = '../ProjectInfo/e';
            }else{
                subtext = '【总金额：'+needCost + '】';
            }
            var myChart = echarts.init(document.getElementById(eleId));
            var option = {
                title : {
                    text: projectName+'\n预计尚需成本统计',
                    subtext:subtext,
                    x:'center',
                    sublink: subLink,
                    subtarget:'self'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox : {
                    show : true,
                    feature : {

                        selfButtons:{//自定义按钮 danielinbiti,这里增加，selfbuttons可以随便取名字
                            show:true,//是否显示
                            title:'自定义', //鼠标移动上去显示的文字
                            icon:'test.png', //图标
                            option:{},
                            onclick:function(option1) {//点击事件,这里的option1是chart的option信息
                                alert('1');//这里可以加入自己的处理代码，切换不同的图形
                            }
                        }
                    }
                },
                series : [
                    {
                        name: '',
                        type: 'pie',
                        radius : '45%',
                        center: ['27%', '52%'],
                        data:seriesData1,
                        itemStyle:{
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} : {c} \n({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        }
                    }, {
                        name: '',
                        type: 'pie',
                        radius : '45%',
                        center: ['75%', '52%'],
                        data:seriesData2,
                        itemStyle:{
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} : {c} ({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        }
                    }
                ]
            };
            myChart.setOption(option);
        };
    });
</script>
</body>
</html>